<?php
/**
 * Created by PhpStorm.
 * User: u
 * Date: 2018/12/11
 * Time: 15:33
 */
namespace common\logics\shop;

use common\widgets\Tool;
use yii\base\BaseObject;
use Yii;

class ShopLogic extends BaseObject implements IShop
{
    /** @var \common\sources\write\shop\ShopAccess $WShopAccess ***/
    private $WShopAccess;
    /** @var \common\sources\read\buyuser\BuyUserAccess $RBuyUserAccess */
    private $RBuyUserAccess;
    /** @var \common\sources\read\shop\ShopAccess $RShopAccess **/
    private $RShopAccess;
    /** @var \common\sources\read\regqrcode\RegQrCodeAccess $RRegQrCodeAccess */
    private  $RRegQrCodeAccess;
    /** @var \common\sources\write\regcode\RegCodeAccess $WRegCodeAccess **/
    private $WRegCodeAccess;
    /** @var \common\sources\read\regcode\RegCodeAccess $RRegCodeAccess **/
    private $RRegCodeAccess;

    public function init()
    {
        $this->WShopAccess = Yii::$container->get("WShopAccess");
        $this->RBuyUserAccess = Yii::$container->get("RBuyUserAccess");
        $this->RShopAccess = Yii::$container->get("RShopAccess");
        $this->RRegQrCodeAccess = Yii::$container->get("RRegQrCodeAccess");
        $this->WRegCodeAccess = Yii::$container->get("WRegCodeAccess");
        $this->RRegCodeAccess = Yii::$container->get("RRegCodeAccess");
        parent::init(); // TODO: Change the autogenerated stub
    }

    public function addShop($data)
    {
        if(empty($data['bus_id'])){
            return Tool::returnJson([],401,"商家id不能为空");
        }
        if(empty($data['shop_type'])){
            return Tool::returnJson([],402,"绑定店铺类型不能为空");
        }
        if(empty($data['cate_id'])){
            return Tool::returnJson([],403,"类别名称id不能为空");
        }
        if(empty($data['cate_name'])){
            return Tool::returnJson([],405,"类别名称不能为空");
        }
        if(empty($data['good_type_id'])){
            return Tool::returnJson([],406,"商品种类id不能为空");
        }
        if(empty($data['good_type_name'])){
            return Tool::returnJson([],407,"商品种类名称不能为空");
        }
        if(empty($data['home_url'])){
            return Tool::returnJson([],408,"店铺首页网址不能为空");
        }
        if(empty($data['ww_name'])){
            return Tool::returnJson([],409,"店铺旺旺名不能为空");
        }
        if(empty($data['shop_name'])){
            return Tool::returnJson([],410,"店铺名称不能为空");
        }
        if(empty($data['good_url'])){
            return Tool::returnJson([],411,"店铺商品链接不能为空");
        }
        if(empty($data['verify_code'])){
            return Tool::returnJson([],412,"验证码不能为空");
        }
        if(empty($data['send_name'])){
            return Tool::returnJson([],413,"发件姓名不能为空");
        }
        if(empty($data['send_phone'])){
            return Tool::returnJson([],415,"商家id不能为空");
        }
        if(empty($data['pro_name'])){
            return Tool::returnJson([],416,"省不能为空");
        }
        if(empty($data['pro_code'])){
            return Tool::returnJson([],417,"省代号不能为空");
        }
        if(empty($data['city_name'])){
            return Tool::returnJson([],418,"市名称不能为空");
        }
        if(empty($data['city_code'])){
            return Tool::returnJson([],419,"市代号不能为空");
        }
        if(empty($data['area_name'])){
            return Tool::returnJson([],420,"地区名称不能为空");
        }
        if(empty($data['area_code'])){
            return Tool::returnJson([],421,"地区代号不能为空");
        }
        if(empty($data['addr'])){
            return Tool::returnJson([],425,"详细地址不能为空");
        }
        //验证商家是否存在  根据商家id 去获取
        $buy = $this->RBuyUserAccess->getBuyUserInfo($data['bus_id']);
        if(empty($buy)){
            return Tool::returnJson([],423,"商家会员不存在,请先注册");
        }
        if($buy['role'] !=2){
            return Tool::returnJson([],424,"你的账号非商家用户,不能绑定店铺");
        }
        if(!Tool::isPhone($data['send_phone']))
        {
            return Tool::returnJson([],425,"手机号码格式不对");
        }
        $shop = $this->RShopAccess->getShopMessage($data);
        if(!empty($shop))
        {
            return Tool::returnJson([],426,"该账号的店铺或者旺旺名已经存在,请重新选择店铺或者旺旺名");
        }
        //绑定店铺限制
        $shop_count = $this->RShopAccess->getShopCount($data);
        if($shop_count>2)
        {
            return Tool::returnJson([],427,"绑定店铺数量不得超过3个");
        }
        //检验验证码是否正确

        $transaction = Yii::$app->db->beginTransaction();
        try{
            $result = $this->WShopAccess->addShop($data);
            if($result){
                $transaction->commit();
                return Tool::returnJson([],200,"绑定店铺成功");
            }
            $transaction->rollBack();
            return Tool::returnJson([],422,"绑定店铺失败");
        }catch (Exception $e){
            $transaction->rollBack();
            return Tool::returnJson([],6000,"入库异常：".$e->getMessage());
        }
    }
    //店铺列表
    public function getList($data,$param)
    {
        if(empty($data['bus_id'])){
            return Tool::returnJson([],401,"商家id不能为空");
        }
//        if(empty($data['shop_type'])){
//            return Tool::returnJson([],402,"店铺类型不能为空");
//        }
        //验证商家是否存在  根据商家id 去获取
        $buy = $this->RBuyUserAccess->getBuyUserInfo($data['bus_id']);
        if(empty($buy)){
            return Tool::returnJson([],403,"商家会员不存在,请先注册");
        }
        if($buy['role'] !=2){
            return Tool::returnJson([],405,"你的账号非商家用户,不能绑定店铺");
        }
        $list = $this->RShopAccess->getList($data);
        if(count($list) > 0){
            foreach ($list as &$item){
                $item['audit_status'] = isset($param['audit_status'][$item['audit_status']]) ? $param['audit_status'][$item['audit_status']] :'待审核';
            }
        }
        return Tool::returnJson($list,200,"成功");
    }

    //获取注册二维码
    public function GetRegQrCode(){
        $qrCode = $this->RRegQrCodeAccess->getRegQrCode();
        if (!$qrCode) {
            $qrCode = '';
        }
        return Tool::returnJson($qrCode,200,'成功');
    }

    //注册码生成
    public function createRegCode()
    {
        $arr = [];
        while (true)
        {
            $code = Tool::createRandNum(6);
            if(!in_array($code,$arr)){
                array_push($arr,$code);
            }
            if(count($arr)>=200)
            {
                break;
            }
        }
        $str = implode(',',$arr);
        $data['reg_code_value'] = 'reg_code_value';
        $data['config_value'] = $str;
        $result = $this->WRegCodeAccess->createRegCode($data);
        return Tool::returnJson($result,200,"成功");
    }

    //获取注册码
    public function getRegCode()
    {
        //验证注册码
        $reg_codes = $this->RRegCodeAccess->getRegCode("reg_code_value");
        $regArr = explode(',',$reg_codes['config_value']);
        return Tool::returnJson($regArr[rand(0,200)],200,"成功");
    }
}